package com.tyqx.hashchainbackend.service.impl;

import com.tyqx.hashchainbackend.service.TrackDonateService;
import com.tyqx.hashchainbean.module.*;
import com.tyqx.hashchaindao.mapper.*;
import com.tyqx.hashchainutil.common.ChainCode;
import com.tyqx.hashchainutil.org.app.config.Config;
import com.tyqx.hashchainutil.org.app.user.UserContext;
import com.tyqx.hashchainutil.org.app.util.Util;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service("trackDonateService")
public class TrackDonateServiceImpl implements TrackDonateService {

    @Resource
    private ProductInMapper productInMapper;

    @Resource
    private DonateCashMapper donateCashMapper;

    @Resource
    private DonateProductMapper donateProductMapper;

    @Resource
    private NeedProductMapper needProductMapper;


    @Resource
    private ProducePlanMapper producePlanMapper;

    @Resource
    private ProduceProductInMapper produceProductInMapper;

    @Resource
    private ProduceProductOutMapper produceProductOutMapper;

    @Resource
    private ProductOutMapper productOutMapper;

    @Resource
    private ShippingInfoMapper shippingInfoMapper;

    @Resource
    private TrackUserMapper trackUserMapper;

    @Resource
    private UseProductMapper useProductMapper;

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private BuyProductMapper buyProductMapper;



    public Integer donateProduct(DonateProduct donateProduct) throws Exception {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        donateProduct.setCreateTime(createTime);
        donateProduct.setUpdateTime(createTime);
        String[] donateIds=donateProduct.getDonateId().split(";");

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        Integer count=0;

        if(donateIds.length>1){
            for (int i=0;i< donateIds.length;i++){
                donateProduct.setDonateId(donateIds[i]);
                JSONObject jsonObject=new JSONObject(donateProduct.toString());
                HashIndex hashIndex= ChainCode.insertHash(jsonObject,userContext);
                donateProduct.setHashValue(hashIndex.getHashValue());
                count=donateProductMapper.addProduct(donateProduct);
            }
        }else{
            donateProduct.setDonateId(donateIds[0]);
            JSONObject jsonObject=new JSONObject(donateProduct.toString());
            HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);
            donateProduct.setHashValue(hashIndex.getHashValue());
            count=donateProductMapper.addProduct(donateProduct);
        }
        return count;
    }


    public Integer donateCash(DonateCash donateCash) throws Exception {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        donateCash.setCreateTime(createTime);
        donateCash.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(donateCash.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        donateCash.setHashValue(hashIndex.getHashValue());

        return donateCashMapper.addCash(donateCash);
    }


    public Integer productIn(ProductIn productIn)throws Exception {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        productIn.setCreateTime(createTime);
        productIn.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(productIn.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        productIn.setHashValue(hashIndex.getHashValue());

        return productInMapper.addInProduct(productIn);
    }


    public Integer productOut(ProductOut productOut) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        productOut.setCreateTime(createTime);
        productOut.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(productOut.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        productOut.setHashValue(hashIndex.getHashValue());

        return productOutMapper.addOutProduct(productOut);
    }


    public Integer useProduct(UseProduct useProduct) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        useProduct.setCreateTime(createTime);
        useProduct.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(useProduct.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        useProduct.setHashValue(hashIndex.getHashValue());

        return useProductMapper.addUseProduct(useProduct);
    }


    public Integer needProduct(NeedProduct needProduct) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        needProduct.setCreateTime(createTime);
        needProduct.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(needProduct.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        needProduct.setHashValue(hashIndex.getHashValue());

        return needProductMapper.addNeedProduct(needProduct);
    }


    public Integer buyProduct(BuyProduct buyProduct) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        buyProduct.setCreateTime(createTime);
        buyProduct.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(buyProduct.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        buyProduct.setHashValue(hashIndex.getHashValue());

        return buyProductMapper.addBuyProduct(buyProduct);
    }


    public Integer producePlan(ProducePlan producePlan) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        producePlan.setCreateTime(createTime);
        producePlan.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(producePlan.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        producePlan.setHashValue(hashIndex.getHashValue());

        return producePlanMapper.addProducePlan(producePlan);
    }


    public Integer produceProductIn(ProduceProductIn produceProductIn) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        produceProductIn.setCreateTime(createTime);
        produceProductIn.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(produceProductIn.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        produceProductIn.setHashValue(hashIndex.getHashValue());

        return produceProductInMapper.addProduceProductIn(produceProductIn);
    }


    public Integer produceProductOut(ProduceProductOut produceProductOut) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        produceProductOut.setCreateTime(createTime);
        produceProductOut.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(produceProductOut.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        produceProductOut.setHashValue(hashIndex.getHashValue());

        return produceProductOutMapper.addProduceProductOut(produceProductOut);
    }


    public Integer shippingProduct(ShippingInfo shippingInfo) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        shippingInfo.setCreateTime(createTime);
        shippingInfo.setUpdateTime(createTime);
        JSONObject jsonObject=new JSONObject(shippingInfo.toString());

        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");

        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);

        shippingInfo.setHashValue(hashIndex.getHashValue());

        return shippingInfoMapper.addShippingInfo(shippingInfo);
    }
}
